from common.models import db
from datetime import datetime
from common.models.users import *


class CourseTagModel(db.Model):
    """课程标签"""
    __tablename__="course_tag_tb"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc="id")
    tag_name = db.Column(db.String(32),doc="标签名称")
    sequence = db.Column(db.Integer,doc="展示顺序")
    course = db.relationship("CourseModel",secondary="course_and_tag_tb",backref=db.backref("course_and_tag"))

class CourseTypeModel(db.Model):
    """课程类型"""
    __tablename__="course_type_tb"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc="id")
    name = db.Column(db.String(32),doc="类别名称")
    course = db.relationship("CourseModel", backref="type")

class CourseModel(db.Model):
    """课程表"""
    __tablename__="course_tb"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc="id")
    name = db.Column(db.String(32),doc="课程名称")
    status = db.Column(db.Integer,default=1,doc="1已上线0未上线")
    img = db.Column(db.String(256),doc="课程封面")
    introduce = db.Column(db.Text,doc="课程介绍")
    read_num = db.Column(db.Integer,default=0,doc="学习人数")
    attention = db.Column(db.Integer,default=0,doc="关注人数")
    course_type = db.Column(db.Integer, db.ForeignKey("course_type_tb.id"), doc="课程类型")
    create_time = db.Column(db.DateTime,default=datetime.now(),doc="创建时间")
    collect = db.Column(db.Integer,doc='是否收藏(0,收藏,1,取消收藏)')
    chapter = db.relationship("ChapterModel", backref="chapter_course")
    comment = db.relationship("CommentModel", backref="comment_course")

class CourseAndTagModel(db.Model):
    """课程表和标签表的中间表"""
    __tablename__ = "course_and_tag_tb"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="id")
    course_id = db.Column(db.Integer,db.ForeignKey("course_tb.id"),primary_key=True, doc="课程id")
    tag_id = db.Column(db.Integer,db.ForeignKey("course_tag_tb.id"), primary_key=True, doc="标签id")


class ChapterModel(db.Model):
    """课程的章节"""
    __tablename__="chapter_tb"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="id")
    title = db.Column(db.String(32), doc="课程名称")
    serial_num = db.Column(db.Integer, doc="展示顺序")
    course_type = db.Column(db.Integer, db.ForeignKey("course_tb.id"), doc="课程类型")
    section = db.relationship("SectionsModel", backref="section_chapter")

class SectionsModel(db.Model):
    """课程的小节"""
    __tablename__="section_tb"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="id")
    section_title = db.Column(db.String(32), doc="小节名称")
    chapter = db.Column(db.Integer, db.ForeignKey("chapter_tb.id"), doc="所属章节")
    serial_num = db.Column(db.Integer, doc="展示顺序")
    learn_time = db.Column(db.Float,default=0.00, doc="视频时长")
    video_url = db.Column(db.String(256),nullable=True, doc="视频地址")
    like_count = db.Column(db.Integer,default=0, doc="点赞次数")

class CommentModel(db.Model):
    """评论表"""
    __tablename__ = "comments_tb"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="id")
    user_id = db.Column(db.Integer, db.ForeignKey("tb_user.uid"), doc="评论用户")
    course_id = db.Column(db.Integer, db.ForeignKey("course_tb.id"), doc="评论的课程")
    create_time = db.Column(db.DateTime,default=datetime.now(),doc="评论时间")
    content = db.Column(db.String(256),doc="评论内容")
    like_count = db.Column(db.Integer,default=0,doc="点赞次数")
    collect = db.Column(db.Integer,doc='是否点赞(0,点赞,1,取消点赞)')
    parent_id = db.Column(db.Integer, db.ForeignKey("comments_tb.id"),nullable=True, doc="回复的评论")
    reply_count = db.Column(db.Integer,default=0,doc="回复数")



# class CommentLiking(db.Model):
#     """
#     评论点赞
#     """
#     __tablename__ = 'comment_like'
#
#     liking_id = db.Column(db.Integer, primary_key=True,autoincrement=True, doc='id')
#     user_id = db.Column(db.Integer, db.ForeignKey("tb_user.uid"), doc="评论用户id")
#     parent_id = db.Column(db.Integer, db.ForeignKey("comments_tb.id"),nullable=True, doc="评论id")
#     ctime = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
#     is_deleted = db.Column(db.Boolean, default=False, doc='是否删除')
























